package chat

import (
	"context"
	"time"
	"xr-game-server/core/constants/cmd"
	"xr-game-server/core/httpserver"
	"xr-game-server/core/push"
	"xr-game-server/dao/roledao"
	"xr-game-server/dto/chatdto"
	"xr-game-server/errercode"
)

func P2PChat(ctx context.Context, req *chatdto.P2PChatReq) (bool, error) {
	roleId := httpserver.GetAuthId(ctx)
	cd := chkCD(roleId, P2P)
	if cd != nil && time.Now().Before(*cd) {
		return false, errercode.CreateCode(errercode.ChatCD)
	}
	roleData := roledao.GetRoleByRole(roleId)
	pushDto := chatdto.NewPushChatDto(roleData, req.Content, req.Param)
	push.WithIds([]uint64{req.TargetId, roleId}, cmd.P2PChat, pushDto)
	flushCD(roleId, P2P)
	return true, nil
}
